'use strict';

    angular.module('app')
        .controller('userAdvertsDetailController', ['$scope', '$rootScope', '$state', '$timeout',
                                             function($scope, $rootScope, $state, $timeout) {
        var title = "广告主详情";
        var id = $state.params.id;
        activate(id);
        $scope.title = $rootScope.title = title;
        
        $scope.loading = true;

        // 初始化页面
		 $scope.businessLicenseArr = [];
		 function activate(id) {
			 $scope.loading = true;
			 $.ajax({
				 url : $scope.app.project_name + '/userAdverts/read/detail',
				 data: {'id': id}
			 }).then(function(result) {
				 $scope.loading = false;
				 if (result.httpCode == 200) {
					 $scope.record = result.data;
					 console.log($scope.record);
					 var businessLicense = result.data.businessLicense;
					 if (businessLicense != null && businessLicense != '') {
						 $scope.$apply(function(){
							 $scope.businessLicenseArr = businessLicense.split('|');
						 })
					 }
				 } else {
					 $scope.msg = result.msg;
				 }
				 $scope.$apply();
			 });
		 }

         $scope.updateService = function(id,serviceAgreementRate) {
                 var serviceAgreementRate = $("#serviceAgreementRate").val();
                 if(serviceAgreementRate==""|| serviceAgreementRate==null){
                     $scope.alertModal({"content":"服务协议费率不能为空"});
                     return;
                 }
                 $.ajax({
                     url : $scope.app.project_name + '/userAdverts/update',
                     data: {'id': id, 'serviceAgreementRate' : serviceAgreementRate}
                 }).then(function(result) {
                     $scope.loading = false;
                     if (result.httpCode == 200) {
                         $scope.alertModal({"content":"操作成功"});
                         window.location.reload();
                     } else {
                         $scope.alertModal({"content":"操作失败"});
                         $scope.msg = result.msg;
                     }
                     $scope.$apply();
                 });
             }

		 $scope.approve = function(id, state) {
		     // alert( $("#utmSource").val());
		     // var utmSource = $("#utmSource").val();

		 	if(state !='3') {
		 	    $scope.alertMessage({
                text:"确定做此操作吗？", //内容
                btnCancel:true, //取消按钮
                btnConfirm:true //确认按钮
            }).then(function () {
                console.log("===========");
                    $("#btn").attr("disabled", true);
                $.ajax({
                    url : $scope.app.project_name + '/userAdverts/update/approve',
                    data: {'ids': id, 'state' : state}
                }).then(function(result) {
                    $scope.loading = false;
                    console.log(result);
                    if (result.httpCode == 200) {
                       if("00000"==result.errorCode){
                           $scope.alertModal({"content":result.errorMsg});
                           $state.go('main.user.userAdverts.list');
                        }else{
                           $scope.alertModal_slow({"content":result.errorMsg});
                           $("#btn").attr("disabled", false);
                       }
                    } else {
                        $("#btn").attr("disabled", false);
                        $scope.alertModal({"content":"请求失败！"});
                    }
                    $scope.$apply();
                });
            })} else {
            	$("#btn").attr("disabled", true);
                var approveReason = $("#approveReason").val();
                if(approveReason==""|| approveReason==null){
                    $scope.alertModal({"content":"审核失败原因不能为空"});
                    return;
                }else{
                	if(approveReason.length>150){
                		$scope.alertModal({"content":"内容不能超过150个字"});
                        return;
                	}
                }

                $.ajax({
                    url : $scope.app.project_name + '/userAdverts/update/approve',
                    data: {'ids': id, 'state' : state, 'approveReason' : approveReason}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.alertModal({"content":"操作成功"});
                        $state.go('main.user.userAdverts.list');
                    } else {
                        $scope.msg = result.msg;
                        $("#btn").attr("disabled", false);
                    }
                });
			}

		 }

		 $scope.preview = function(){
             // $("#utmSource").val(1);
			 $scope.firstApply = true;
		 }

         $scope.approveService = function(){
             // $scope.firstApply1 = true;
         }

    }]);